// import { asyncRoutes, constantRoutes } from '@/router'
import { constantRoutes } from '@/router'
import Layout from '@/layout'

/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */
function hasPermission(roles, route) {
  if (route.meta && route.meta.roles) {
    return roles.some(role => route.meta.roles.includes(role))
  } else {
    return true
  }
}

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */

export function returnPath(path) {
  return `@/views/${path}/detail`
}

export function filterAsyncRoutes(routes, roles) {
  const res = []

  routes.forEach(route => {
    const tmp = { ...route }
    if (hasPermission(roles, tmp)) {
      if (tmp.children) {
        tmp.children = filterAsyncRoutes(tmp.children, roles)
      }
      res.push(tmp)
    }
  })

  return res
}

const state = {
  routes: [],
  addRoutes: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
  }
}

const actions = {
  generateRoutes({ commit }, Obj) {
    console.log(Obj.routers)
    return new Promise(resolve => {
      const accessedRoutes = Obj.routers
      const userRouter = accessedRoutes.map(item => {
        const childArr = item.subMenuList
        let route = {}
        if (item.path !== 'contact') {
          route = {
            level: 1,
            id: item.id,
            canadd: true,
            path: '/' + item.path,
            component: Layout,
            redirect: '/' + item.path,
            name: item.path,
            idPath: item.path,
            alwaysShow: true,
            meta: {
              title: item.name,
              icon: 'nested'
            },
            children: []
          }
          if (childArr.length > 0) {
            const path = item.path

            route.children = childArr.map(child => {
              // const componentTxt = `@/views/${item.path}/detail`
              // const componentTxt = '../../views/news/detail'
              // const componentObj = import(componentTxt)
              // console.log(componentObj)
              if (path) {
                return {
                  parentId: item.id,
                  id: child.id,
                  path: item.path + child.path,
                  idPath: child.path,
                  component: resolve => require([`@/views/${item.path}/detail`], resolve),
                  // component: resolve => require([`@/views/solution/detail`], resolve),
                  name: item.path + child.path,
                  meta: { title: child.name },
                  roles: ['admin'] // or you can only set roles in sub nav
                }
              }
            })
          } else {
            route.children = []
          }
          route.children.unshift({
            parentId: item.id,
            id: 'all',
            path: item.path + 'all',
            idPath: item.path + 'all',
            component: resolve => require([`@/views/${item.path}/all`], resolve),
            // component: resolve => require([`@/views/solution/detail`], resolve),
            name: item.path + 'all',
            meta: { title: '全部内容' },
            roles: ['admin'] // or you can only set roles in sub nav
          })
        } else {
          route = {
            level: 1,
            path: `/${item.path}`,
            component: Layout,
            redirect: `/${item.path}`,
            children: [
              {
                id: item.id,
                path: item.path,
                idPath: item.path,
                component: resolve => require([`@/views/${item.path}/index`], resolve),
                name: item.path,
                meta: { title: item.name, icon: 'nested' }
              }
            ]
          }
        }

        return route
      })
      // const arr
      // userRouter.map(res => {
      //   console.log(res)
      // })
      userRouter.push({
        level: 1,
        path: '/tellme',
        component: Layout,
        redirect: '/tellme',
        children: [
          {
            path: 'tellme',
            component: () => import('@/views/tellme/index'),
            name: 'tellme',
            meta: { title: '投诉建议', icon: 'nested' }
          }
        ]
      })
      commit('SET_ROUTES', userRouter)
      resolve(userRouter)
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
